package com.omri.when.there;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Second extends Activity implements OnClickListener {

	EditText etWhat;
	TextView tvDescription;
	EditText etDescription;
	TextView tvRepeated;
	RadioGroup rgReptead;
	RadioButton rRepeat1;
	RadioButton rRepeat2;
	TextView tvAddress;
	Button bSave1;
	String gotAddressLng;
	String gotAddressLat;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		initialize();
		Bundle gotBasket = getIntent().getExtras();
		String gotAddress = gotBasket.getString("address");
		gotAddressLng = gotBasket.getString("longitude");
		gotAddressLat = gotBasket.getString("latitude");
		tvAddress.setText(gotAddress);
		bSave1.setOnClickListener(this);
	}

	private void initialize() {
		// TODO Auto-generated method stub
		tvAddress = (TextView) findViewById(R.id.tvAddress);
		etWhat = (EditText) findViewById(R.id.etWhat);
		etDescription = (EditText) findViewById(R.id.etDescription);
		tvRepeated = (TextView) findViewById(R.id.tvRepeated);
		rgReptead = (RadioGroup) findViewById(R.id.rgReptead);
		rRepeat1 = (RadioButton) findViewById(R.id.rRepeat1);
		rRepeat2 = (RadioButton) findViewById(R.id.rRepeat2);
		bSave1 = (Button) findViewById(R.id.bSave1);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bSave1:
			Boolean didItworked = true;
			try {
				String Title = etWhat.getText().toString();
				String Description = etDescription.getText().toString();
				Float Latitude = Float.parseFloat(gotAddressLat);
				Float Longitude = Float.parseFloat(gotAddressLng);

				Db entry = new Db(Second.this);
				entry.write();
				entry.createEntry(Title, Description, Latitude, Longitude);
				entry.close();
			} catch (Exception e) {
				didItworked = false;
			} finally {
				if (didItworked) {
					/*
					 * Dialog d = new Dialog(this); d.setTitle("Saved!");
					 * TextView tv = new TextView(this);
					 * tv.setText("You can edit your reminder at Saved reminder tab"
					 * ); d.setContentView(tv); d.show();
					 */
					Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
					Intent i = new Intent("com.omri.when.there.SAVEDREMINDERS");
					startActivity(i);
				}
			}
		}
	}
}